package com.derbtec.refale.activities;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ListView;

import com.derbtec.refale.R;
import com.derbtec.refale.models.Event;
import com.derbtec.refale.transaction.GetEventParticipantsAsyncTask;
import com.derbtec.refale.transaction.GetEventsAsyncTask;

public class EventListViewActivity extends ListActivity {

	private ArrayList<Event> elements = new ArrayList<Event>();
	private EventListViewAdapter adapter;
	private EditText txtFromDate;
	private EditText txtToDate;
	private EditText txtComment;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
		{
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
		
		setContentView(R.layout.activity_event_list_view);

		adapter = new EventListViewAdapter(this, elements);
		setListAdapter(adapter);
		
		GetEventsAsyncTask geteventsasynctask = new GetEventsAsyncTask(EventListViewActivity.this);
		geteventsasynctask.execute();
		
	}

	public void updateData() {
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		GetEventParticipantsAsyncTask getEventParticAsynctask = new GetEventParticipantsAsyncTask(EventListViewActivity.this, elements.get(position).getId(), position);
		getEventParticAsynctask.execute();

	}

	public void startDetailViewActivity(int position, int participantscount, boolean participantself) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		Intent eventDetail = new Intent(EventListViewActivity.this, EventDetailViewActivity.class);
		eventDetail.putExtra("eventname", elements.get(position).getName());
		eventDetail.putExtra("eventcreator", elements.get(position).getCreator().getUsername());
		eventDetail.putExtra("eventlastedit", sdf.format(elements.get(position).getCreationDate()));
		eventDetail.putExtra("eventstart", sdf.format(elements.get(position).getStartDate()));
		eventDetail.putExtra("eventend", sdf.format(elements.get(position).getEndDate()));
		eventDetail.putExtra("eventplace", elements.get(position).getPlace());
		eventDetail.putExtra("eventdescription", elements.get(position).getDescription());
		eventDetail.putExtra("eventid", (elements.get(position)).getId());
		eventDetail.putExtra("eventcreatorid", elements.get(position).getCreator().getId());
		eventDetail.putExtra("participantscount", participantscount);
		eventDetail.putExtra("participantself", participantself);
		eventDetail.putExtra("admin", elements.get(position).getCreator().isAdmin());

		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        parentActivity.startChildActivity("EventDetailActivity", eventDetail);
	}

	public EditText getTxtFromDate() {
		return txtFromDate;
	}

	public void setTxtFromDate(EditText txtFromDate) {
		this.txtFromDate = txtFromDate;
	}

	public ArrayList<Event> getElements() {
		return elements;
	}

	public void setElements(ArrayList<Event> elements) {
		this.elements = elements;
	}
	
	public void addElements(ArrayList<Event> elements) {
		this.elements.addAll(elements);
	}

	public EditText getTxtToDate() {
		return txtToDate;
	}

	public void setTxtToDate(EditText txtToDate) {
		this.txtToDate = txtToDate;
	}

	public EditText getTxtComment() {
		return txtComment;
	}

	public void setTxtComment(EditText txtComment) {
		this.txtComment = txtComment;
	}

}
